package com.nakamachizu.util;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.slim3.controller.validator.Errors;

public class ErrorsUtil {

    public static void putErrors(Errors errors, ResourceBundle bundle, String bundleKey, String errorKey, Object... args){
        String pattern = bundle.getString(bundleKey);
        errors.put(errorKey, MessageFormat.format(pattern, args));
        //return errors;
    }
    
    public static void putErrors(Errors errors, HttpServletRequest request, String bundleKey, String errorKey, Object... args){
        ResourceBundle bundle = ResourceUtil.getResourceBundle(request);
        String pattern = bundle.getString(bundleKey);
        errors.put(errorKey, MessageFormat.format(pattern, args));
        //return errors;
    }
    
    public static String stackTrace(Throwable error){
        StringBuffer buf = new StringBuffer("");

        buf.append(error.getClass().getSimpleName()).append(": ").append(error.getMessage()).append("\n");
        StackTraceElement[] list = error.getStackTrace();
        for(StackTraceElement ste : list){
            buf.append(ste.toString()).append("\n");
        }
        return buf.toString();
    }
}